package com.ruoyi.api.controller;

import com.ruoyi.api.dto.CommonDTO;
import com.ruoyi.api.dto.StoreDTO;
import com.ruoyi.api.dto.StoreUpdateDTO;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.aliyun.FileUploadOssUtils;
import com.ruoyi.system.service.IStoreService;
import com.ruoyi.system.service.ITCarouselService;
import com.ruoyi.user.dto.ApiUserVo;
import com.ruoyi.user.dto.UploadAvatarDTO;
import com.ruoyi.user.exception.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;

@Api(tags = "APP店铺管理")
@RestController
@RequestMapping("/api/store")
public class ApiStoreController extends ApiBaseController{

    @Autowired
    private IStoreService IStoreService;

    /**
     * 店铺列表接口
     * @param
     */
    @GetMapping("/list")
    @ApiOperation("店铺列表接口")
    public AjaxResult list () {
        ApiUserVo apiUserVo = checkLoginAndGetMember();
        return AjaxResult.success(IStoreService.selectStoreListByUserId(apiUserVo.getUserId()));
    }

    /**
     * 修改联系人接口
     * @param
     */
    @PostMapping("/updateStoreMaster")
    @ApiOperation("设置店铺为当前主店铺接口")
    public AjaxResult updateStoreMaster (StoreUpdateDTO updateDTO) {
        return AjaxResult.success(IStoreService.updateStoreMaster(updateDTO.getStoreId()));
    }

    /**
     * 修改联系人接口
     * @param
     */
    @PostMapping("/updateStoreTel")
    @ApiOperation("修改店铺电话接口")
    @Deprecated
    public AjaxResult updateStoreTel (StoreUpdateDTO updateDTO) {
        return AjaxResult.success(IStoreService.updateStoreTel(updateDTO.getParamStr(),updateDTO.getStoreId()));
    }

    /**
     * 修改联系人接口
     * @param
     */
    @PostMapping("/updateStoreContact")
    @ApiOperation("修改店铺联系人接口")
    @Deprecated
    public AjaxResult updateStoreContact (StoreUpdateDTO updateDTO) {
        return AjaxResult.success(IStoreService.updateStoreContact(updateDTO.getParamStr(),updateDTO.getStoreId()));
    }

    /**
     * 修改联系人接口
     * @param
     */
    @PostMapping("/updateStoreAddress")
    @ApiOperation("修改店铺地址接口")
    @Deprecated
    public AjaxResult updateStoreAddress (StoreUpdateDTO updateDTO) {
        return AjaxResult.success(IStoreService.updateStoreAddress(updateDTO.getParamStr(),updateDTO.getStoreId()));
    }

    /**
     * 修改店铺信息接口
     * @param
     */
    @PostMapping("/updateStore")
    @ApiOperation("修改店铺信息接口")
    public AjaxResult updateStore (StoreDTO storeDTO) {
        return AjaxResult.success(IStoreService.updateStoreByApp(storeDTO.getId(),
                storeDTO.getStoreName(),storeDTO.getStoreAddress(),storeDTO.getStoreTel(),storeDTO.getContacts(),storeDTO.getImgUrl(),storeDTO.getStoreImg()));
    }

    /**
     * 修改联系人接口
     * @param
     */
    @PostMapping("/updateStoreName")
    @Deprecated
    @ApiOperation("修改店铺名称接口")
    public AjaxResult updateStoreName (StoreUpdateDTO updateDTO) {
        return AjaxResult.success(IStoreService.updateStoreName(updateDTO.getParamStr(),updateDTO.getStoreId()));
    }
    /**
     * 店铺列表接口
     * @param
     */
    @PostMapping("/add")
    @ApiOperation("新增店铺信息")
    public AjaxResult add (StoreDTO storeDTO) {
        ApiUserVo apiUserVo = checkLoginAndGetMember();
        return AjaxResult.success(IStoreService.insertStoreByApp(apiUserVo.getUserId(),
                storeDTO.getStoreName(),storeDTO.getStoreAddress(),storeDTO.getStoreTel(),storeDTO.getContacts(),storeDTO.getImgUrl(),storeDTO.getStoreImg()));
    }

    @PostMapping(value = "/upload/img", consumes = "multipart/form-data")
    @ApiOperation("上传营业执照")
    public AjaxResult uploadAvatar(CommonDTO common){
        checkLogin();
        String url = uploadFile(common.getFile());
        if (StringUtils.isEmpty(url)) {
            return AjaxResult.error(ResultCode.AVATAR_UPLOAD_ERROR.message());
        }
        return AjaxResult.success(url);
    }

    private String uploadFile (MultipartFile file) {
        try {
            String fileName = file.getOriginalFilename();
            String newFileName = String.format("%s-%s", DateUtils.dateTimeNow(), fileName);
            return FileUploadOssUtils.uploadInputStreamToOss(new ByteArrayInputStream(file.getBytes()), "mars/store", newFileName, null);
        } catch (Exception e) {
            return null;
        }
    }
}
